package com.wjx.article_sys.controller;

import com.github.pagehelper.PageInfo;
import com.wjx.article_sys.entity.Article;
import com.wjx.article_sys.service.impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
@Controller
public class HomeController {

    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private HttpSession session;
    @GetMapping("/")
    public String index(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                        @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
                        @RequestParam(name = "contentSize", defaultValue = "40") int contentSize,
                        Model model) {
        if (session.getAttribute("userName") == null) {
            session.setAttribute("userName", "空");
        }
        PageInfo<Article> articlePageInfo = articleService.queryAllArticle(pageNum, pageSize);
        for (Article article : articlePageInfo.getList()) {
            if (article.getContent().length() >= contentSize) {
                article.setContent(article.getContent().substring(0, contentSize) + "......");
            }
        }
        model.addAttribute("articlePageInfo", articlePageInfo);
        if (contentSize == 40)
            return "front/index";
        else
            return "back/articleAdmin";
    }

}
